﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyIM2013.Packets;

namespace TinyIM2013
{
    /// <summary>
    /// 存放用户相关密钥的类
    /// </summary>
    public class IMKeyCollection
    {
        private static IMKeyCollection instance;
        /// <summary>
        /// 账号/TEA collection 的集合
        /// </summary>
        private Dictionary<byte[], Dictionary<Packets.PCmd, byte[]>> AccountDic;
        /// <summary>
        /// 命令名称/TEA key 的集合
        /// </summary>
        private Dictionary<PCmd, byte[]> CommandDic;

        public IMKeyCollection()
        {
            AccountDic = new Dictionary<byte[], Dictionary<Packets.PCmd, byte[]>>();
        }
        public byte[] GetKey(byte[] account, PCmd command)
        {
            if (AccountDic.Count > 0)
            {
                foreach (var item in AccountDic)
                {
                    if (item.Key.SequenceEqual(account))
                    {
                        foreach (var inner in item.Value)
                        {
                            if (inner.Key == command)
                            {
                                return inner.Value;
                            }
                        }
                    }
                }
            }
            return null;
        }
        public void SetKey(byte[] account, PCmd command, byte[] teaKey)
        {
            //取key
            var result = GetKey(account, command);
            if (result != null)//key存在
            {
                if (!GetKey(account, command).SequenceEqual(teaKey))
                {
                    foreach (var item in AccountDic)
                    {
                        item.Value[command] = teaKey;//替换
                    }
                }
            }
            else//key不存在
            {
                //取cmdDic
                var commandDic = GetCommandDicByAccount(account);
                if (commandDic != null)//cmdDic存在
                {
                    //添加
                    commandDic.Add(command, teaKey);
                }
                else
                {
                    //创建并添加
                    commandDic = new Dictionary<PCmd, byte[]>();
                    commandDic.Add(command, teaKey);
                    //添加到父对象
                    AccountDic.Add(account, commandDic);
                }

            }
        }
        
        public Dictionary<PCmd, byte[]> GetCommandDicByAccount(byte[] account)
        {
            if (AccountDic.Count > 0)
            {
                foreach (var keyCollection in AccountDic)
                {
                    if (keyCollection.Key.SequenceEqual(account))
                    {
                        return keyCollection.Value;
                    }
                }
            }
            return null;
        }
        public static IMKeyCollection Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IMKeyCollection();
                }
                return instance;
            }
        }
    }
}
